<?php
namespace Home\Model;
use Common\Model\BaseModel;

class CommentModel extends BaseModel {

    public $tableName = 'comment';

    protected $_validate = array(
        array('contract_id','require','合同编号不能为空!'),
        array('contract_id','','合同编号已经存在！',0,'unique',1),
    );

    /**
     * @param $post
     * @return array
     * 更新或修改合同信息
     */
    public function saveComment($data){

        $ret = array('status'=>true,'msg'=>'');
       
        $post = $this->returnData($data);
     
        if(!$this->create($post)){

            $ret['status'] = false;
            $ret['msg'] = $this->getError();

        }else{

            $res = null !== $post['comment_id'] ? $this->save($post) : $this->add($post);

            if(false !== $res){
                $info = array('comment_id'=>$res);
                $is = D('EquRepairRecords')->where('equ_repair_records_id='.$data['equ_repair_records_id'])->setField($info);
                if(false !==$is){
                    $ret['msg'] = '操作成功';
                }else{
                    $ret['status'] = false;
                    $ret['msg'] = $this->getDbError();
                }
            }else{
                $ret['status'] = false;
                $ret['msg'] = $this->getDbError();
            }
        }

         return $ret;
    }

    public function returnData($post){

        $return = array();

        $return['comment_id']       = !empty($post['comment_id']) ? addslashes($post['comment_id']) : null;
        $return['score']            =  $this->getScore($post);
        $return['time']             = !empty($post['make_time']) ? strtotime($post['make_time']) : time();
        $return['user_id']          = !empty($post['user_id']) ? intval($post['user_id']) : 0;
        $return['reg_time']         = !empty($post['reg_time']) ? strtotime($post['reg_time']) : time();
        $return['source_user_id']   = !empty($post['source_user_id']) ? intval($post['source_user_id']) : $_SESSION['USER']['INFO']['user_id'];
        $return['ext']              = $this->getExt($post);
        $return['del']              = 1;

        return $return;
    }

    public function getExt($data){
        
        $return = array();
        $count = 0;
        foreach ($data as $key => $value) {
            if('question' == substr($key,0,8)){
                
                $question_id = explode('_',$key);
                $return['question_list'][$count][$question_id[1]] = $value;
                $count++;
            }
        }
        $return['question_num'] = $count++;
        return json_encode($return);
    }
    public function getScore($question){

        $total_score = 0;
        $count = 0;
        foreach ($question as $key => $value) {
            if('question' == substr($key,0,8)){
                $total_score += intval($value);
                $count++;
            }
        }
        return $total_score/$count;
    }
}